home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Science
/
Asymptote Demo
/
Examples
/
Utility Scripts
/
polyfit
< prev
next >
Wrap
Text File
|
1994-05-07
|
1KB
|
40 lines
% polyfit order xmin xmax dospline plotline
%
% Calculate a polynomial fit of up to order 5, replace the y vector with the
% fit calculated at each point and (optionally) plot the fit.
%
% order - order of polynomial to fit (1,2,3,4,5)
% xmin - minimum x to include in fit
% xmax - maximum x to include in fit
% • Note: if xmin = xmax this script won't alter the e vector.
% You should supply your own weights in this case
% dospline - if TRUE, then spline the fit before plotting
% plotline - if TRUE, then plot the fitted function as a line
% If xmax ≠ xmin, then calculate the weights (errors) for the e vector.
if &2 &3 ≠ then rpn <x &2 < <x &3 > + 1 * <x &2 > <x &3 < * 0.0001 * + >e
% Perform the fit.
fitxy &1
% Calculate the fit on the stack
rpn a0 <x a1 * +
if &1 2 >= then rpn <x dup * a2 * +
if &1 3 >= then rpn <x 3 y^x a3 * +
if &1 4 >= then rpn <x 4 y^x a4 * +
if &1 5 >= then rpn <x 5 y^x a5 * +
% Calculate the rms of the fit residuals.
% (This is optional--for speed you can commment these lines out.)
rpn dup <y - rms
set fit_rms v0(1)
rpn drop drop
% Put the calculated fit in the y vector:
rpn >y
% Calculate a spline and plot a line if requested to do so.
if &4 then spline
if &5 then plotline